﻿using System;
using System.Collections.Generic;
using System.Text;
using GlobalHelper;
using TimeOperator;
using IBDM2IBSInterface;


namespace Models
{
    public class DepartureModel
    {
        public int Id { get; set; }
        public int Time { get; set; }
        public IStation Station { get; set; }
        public IStation ToStation { get; set; }
        public DepartureTypeModel Type { get; set; }


        public DepartureModel() { }

        public PartModel OwnPart { get; set; }
        public TurnModel OwnTurn { get; set; }

        public DepartureModel(int time, IStation station,IStation toStation,DepartureTypeModel type)
        {
            Time = time;
            Station = station;
            ToStation = toStation;
            Type = type;
        }

        public DepartureModel(int time, IStation station, IStation toStation, DepartureTypeModel type, PartModel partModel, TurnModel turnModel)
        {
            Time = time;
            Station = station;
            ToStation = toStation;
            Type = type;
            OwnPart = partModel;
            OwnTurn = turnModel;
        }

        ///// <summary>
        ///// 1. 判断 发车时间 是否满足“合理时间”
        ///// </summary>
        ///// <returns></returns>
        //public virtual bool CheckRule()
        //{
        //    if (OwnTurn.OwnTimeSheet._baseDataProvider == null)
        //    {
        //        return false;
        //    }
        //    bool result = false;

        //    //上行或下行 合理时间
        //    var baseDataProvider = OwnTurn.OwnTimeSheet._baseDataProvider;
        //    int linkTimeLimit = baseDataProvider.GetLinkTime(Time, Direction) + baseDataProvider.TerminalStayTimeLowerLimit;

        //    //if empty
        //    if (OwnPart.Count == 0)
        //    {
        //        return true;
        //    }

        //    //if have item
        //    var firstDep = new HourAndMinute(OwnPart.Dapartures[0].Time);
        //    var lastDep = new HourAndMinute(OwnPart.Dapartures[OwnPart.Dapartures.Count - 1].Time);
        //    var currDep = new HourAndMinute(Time);

        //    //插入在 首
        //    if (firstDep.Bigger(currDep))
        //    {
        //        if (firstDep - currDep >= linkTimeLimit)
        //        {
        //            result = true;
        //        }
        //    }
        //    //插入在 末
        //    else if (currDep.Bigger(lastDep))
        //    {
        //        if (currDep - lastDep >= linkTimeLimit)
        //        {
        //            result = true;
        //        }
        //    }

        //    return result;
        //}

        #region Operator

        public static bool operator <(DepartureModel departure1, DepartureModel departure2)
        {
            bool result = false;

            var time1 = new HourAndMinute(departure1.Time);
            var time2 = new HourAndMinute(departure2.Time);

            if (time2.Bigger(time1))
            {
                result = true;
            }

            return result;
        }

        public static bool operator >(DepartureModel departure1, DepartureModel departure2)
        {
            var result = false;
            result = !(departure1 < departure2 || departure1 == departure2);
            return result;
        }

        public static bool operator ==(DepartureModel departure1, DepartureModel departure2)
        {
            Object nullCheck1 = departure1;
            Object nullCheck2 = departure2;
            if (nullCheck2 == null)
            {
                return nullCheck1 == null ? true : false;
            }

            bool result = false;

            var time1 = new HourAndMinute(departure1.Time);
            var time2 = new HourAndMinute(departure2.Time);

            if (time1.Balance(time2) == 0)
            {
                result = true;
            }

            return result;
        }

        public static bool operator !=(DepartureModel departure1, DepartureModel departure2)
        {
            return !(departure1 == departure2);
        }

        public static int operator -(DepartureModel departure1, DepartureModel departure2)
        {
            int result = 0;

            var time1 = new HourAndMinute(departure1.Time);
            var time2 = new HourAndMinute(departure2.Time);

            result = time1 - time2;

            return result;
        }
        #endregion

        public bool Equals(DepartureModel other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.OwnPart, OwnPart) && Equals(other.OwnTurn, OwnTurn);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(DepartureModel)) return false;
            return Equals((DepartureModel)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((OwnPart != null ? OwnPart.GetHashCode() : 0) * 397) ^ (OwnTurn != null ? OwnTurn.GetHashCode() : 0);
            }
        }
    }
}
